package com.sm.controller;

import com.github.pagehelper.PageInfo;
import com.sm.entity.Attendance;
import com.sm.entity.AttendanceRec;
import com.sm.entity.ResultMap;
import com.sm.entity.UserLogin;
import com.sm.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin/attendance")
public class AttendanceController {

    @Autowired
    ResultMap resultMap;
    @Autowired
    AttendanceService attendanceService;

    /**
     * @return 返回今天考勤比例
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("atnow")
    public ResultMap atNow() throws Exception {
        AttendanceRec counts = attendanceService.atNow();
        resultMap.setResult("counts",counts);
        return resultMap;
    }

    /**
     * @return 返回最近7，8天考勤比例结果
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("atrecently")
    public ResultMap atRecently()throws Exception{
        List<AttendanceRec> attendanceRecs = attendanceService.atRecently();
        resultMap.setResult("atrecs",attendanceRecs);
        return resultMap;
    }

    /**
     * @param uid 员工uid
     * @return 返回该员工该月份的考勤记录
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("searchone")
    public ResultMap searchOne(Integer uid)throws Exception{
//        resultMap.setResult("searchone",attendanceService.searchOneByUid(uid));
        return resultMap;
    }

    /**
     * @return 返回员工打卡结果 上班打卡 下班打卡
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("issign")
    public ResultMap isSign()throws Exception{
        UserLogin userLogin = (UserLogin)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer uid = userLogin.getUid();
        List<Boolean> flags = attendanceService.isSign(uid);
        resultMap.setResult("flags",flags);
        return resultMap;
    }


    /**
     * @param sign 1为上班 2为下班
     * @return 返回无
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("signin")
    public ResultMap signIn(Integer sign)throws Exception{
        UserLogin userLogin = (UserLogin)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer uid = userLogin.getUid();
        System.out.println("atttendanceController: "+uid);
        attendanceService.signIn(uid,sign);
        return resultMap;
    }

    /**
     * @param sign 1为加班1小时 2为 加班2小时
     * @return 返回注意身体
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("overtime")
    public ResultMap addOvertime(Integer sign)throws Exception{
        UserLogin userLogin = (UserLogin)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer uid = userLogin.getUid();
        attendanceService.addOverTime(uid,sign);
        resultMap.setMessage("请注意身体");
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("attable")
    public ResultMap AtTable(Integer uid,String username ,String name)throws Exception{
        if(username.equals(""))username = null;
        if(name.equals(""))name = null;
        List<Attendance> list = attendanceService.atTable(uid,username,name);
        PageInfo<Attendance> result = new PageInfo<Attendance>(list);
        resultMap.setResult("attable",result);
        return resultMap;
    }
}
